package ch.il07.converter.business;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;


public class ServiceFactory {
	private static Map<String, Service> mappingTable = new HashMap<String, Service>(); 
	
	public static Service getInstance(Class<? extends Service> serviceClassToLoad) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		String className = serviceClassToLoad.getSimpleName();
		Service s = mappingTable.get(className);
		if(s != null) {
			return s;
		}
		String serviceImplName = getPackageName() + ".impl." + getServiceClassName(className);
		Object o = Class.forName(serviceImplName).newInstance();
		if(o instanceof Service) {
			s = (Service) o;
			mappingTable.put(className, s);
		}
		return s;
	}
	
	private static String getPackageName() {
		return ServiceFactory.class.getPackage().getName();
	}

	private static String getServiceClassName(String className) {
		ResourceBundle rb = ResourceBundle.getBundle(getPackageName() + ".servicemapping");
		return rb.getString("Service." + className);
	}
}